//
//  HMWelcomeViewController.swift
//  swift微博项目
//
//  Created by huodian on 16/9/27.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       
    setMyUI()
        
    }

    func setMyUI(){
        
       self.view.addSubview(welcomeLabel)
        
        self.view.addSubview(userImage)
        
        userImage.snp.makeConstraints { (make) in
            
            make.top.equalTo(view).offset(10)
            
            make.centerX.equalTo(view)
            
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        
        welcomeLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImage.snp.bottom).offset(20)
            
            make.centerX.equalTo(view)
        }
       
        
        userImage.sd_setImage(with: HMUserAccessViewModel.shareUserAccess.iconUrl)
    }
    //如果修改约束代码放在loadview中则会出现动画从左上角飞入情况
    override func viewDidAppear(_ animated: Bool) {
        
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions.allowAnimatedContent, animations: { 
            
            self.userImage.snp.updateConstraints({ (make) in
                
                make.top.equalTo(self.view).offset(200)
                
            })
            //更新约束不是立即修改frame,frame是在layoutsubview中根据设置好的约束进行修改的
            //提前布局子示图 -> layoutsubview -> 设置动画才会生效
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
              UIView.animate(withDuration: 0.5, animations: { 
                           self.welcomeLabel.alpha = 1
                }, completion: { (_) in
                    
                    //登陆成功,切换控制器
                    //发送通知
                    NotificationCenter.default.post(name: NSNotification.Name(KchangNotification), object: nil)
                    
              })
       
        }
        
        
    }
    
    lazy var userImage : UIImageView = {
        
       let jeje = UIImageView()
        
        jeje.image = UIImage(named: "avatar_default_big")
        
        jeje.layer.cornerRadius = 42
        
        jeje.layer.masksToBounds = true
        
        return jeje
        
        
        
    }()
    
    
    lazy var welcomeLabel : UILabel = {
        
        let l = UILabel(title: "欢迎回来", font: 14, corlor: UIColor.darkGray)
        
        l.alpha = 0
        
        return l
        
        
    }()
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}
